package com.ffapp.demo.bean;


import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import my.FileUtils;

public class HomeGrideListBean extends BaseListBeanBc {


    public static final int MAX_SHOW = 12;

    private String title;
    private String classify;

    private ArrayList<HomeGrideListData> list = new ArrayList<HomeGrideListData>();


    public ArrayList<HomeGrideListData> getList() {
        return list;
    }

    public void setList(ArrayList<HomeGrideListData> list) {
        this.list = list;
    }

    public boolean isMore(){
        return  list.size() > (MAX_SHOW-1);
    }

    public HomeGrideListBean copy(){
        HomeGrideListBean  listBeanCopy = new HomeGrideListBean();
        try {
            List<HomeGrideListData> listcopy =  FileUtils.deepCopy(list);
            ArrayList<HomeGrideListData> listArray = new ArrayList<>();
            for(HomeGrideListData grideListData:listcopy){
                listArray.add(grideListData);
            }
            listBeanCopy.setList(listArray);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return  listBeanCopy;
    }

    public HomeGrideListBean searchByName(String name){
        HomeGrideListBean listBean = new HomeGrideListBean();
        for(HomeGrideListData homeGrideListData:list){
            if(homeGrideListData.getName().contains(name)){
                listBean.getList().add(homeGrideListData);
            }
        }
        return listBean;
    }

    public JSONArray getPostJsonArray(){
        JSONArray jsonArray = new JSONArray();
        int count = 0;
        for(HomeGrideListData grideListData:list){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("moduleCode",grideListData.getId());
            jsonObject.put("moudleOrder",count+"");
            jsonArray.add(jsonObject);
            count++;
        }
        return jsonArray;
    }

    public HomeGrideListData searchDataByName(String name){
        for(HomeGrideListData homeGrideListData:list){
            if(homeGrideListData.getName().contains(name)){
                return homeGrideListData;
            }
        }
        return null;
    }

    public boolean isRightByName(String name){
        return  searchDataByName(name) != null;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getClassify() {
        return classify;
    }

    public void setClassify(String classify) {
        this.classify = classify;
    }


    public HomeAppGroupListBean processGroupListBean(HomeGrideListBean listBeanMine){
        HomeAppGroupListBean listBean = new HomeAppGroupListBean();
        HomeGrideListBean homeGrideListBean ;
        homeGrideListBean  = new HomeGrideListBean();
        homeGrideListBean.setClassify(HomeGrideListData.CLASSIFY_COMMAND);
        homeGrideListBean.setTitle(HomeGrideListData.getClassifyName(HomeGrideListData.CLASSIFY_COMMAND));
        listBean.getList().add(homeGrideListBean);
        homeGrideListBean  = new HomeGrideListBean();
        homeGrideListBean.setClassify(HomeGrideListData.CLASSIFY_COLLECT);
        homeGrideListBean.setTitle(HomeGrideListData.getClassifyName(HomeGrideListData.CLASSIFY_COLLECT));
        listBean.getList().add(homeGrideListBean);
//        homeGrideListBean  = new HomeGrideListBean();
//        homeGrideListBean.setClassify(HomeGrideListData.CLASSIFY_BASEINFO);
//        homeGrideListBean.setTitle(HomeGrideListData.getClassifyName(HomeGrideListData.CLASSIFY_BASEINFO));
//        listBean.getList().add(homeGrideListBean);

        homeGrideListBean  = new HomeGrideListBean();
        homeGrideListBean.setClassify(HomeGrideListData.CLASSIFY_FORMAL_BUILD);
        homeGrideListBean.setTitle(HomeGrideListData.getClassifyName(HomeGrideListData.CLASSIFY_FORMAL_BUILD));
        listBean.getList().add(homeGrideListBean);

        homeGrideListBean  = new HomeGrideListBean();
        homeGrideListBean.setClassify(HomeGrideListData.CLASSIFY_KNOWLEGE);
        homeGrideListBean.setTitle(HomeGrideListData.getClassifyName(HomeGrideListData.CLASSIFY_KNOWLEGE));
        listBean.getList().add(homeGrideListBean);

        homeGrideListBean  = new HomeGrideListBean();
        homeGrideListBean.setClassify(HomeGrideListData.CLASSIFY_OTHER);
        homeGrideListBean.setTitle(HomeGrideListData.getClassifyName(HomeGrideListData.CLASSIFY_OTHER));
        listBean.getList().add(homeGrideListBean);

        for (HomeGrideListData data:list){
            addToGroup(listBean,data,listBeanMine);
        }


        Iterator<HomeGrideListBean> iterator =   listBean.getList().iterator();
        while (iterator.hasNext()){
            HomeGrideListBean  itemData = iterator.next();
            if(itemData.getList().size() == 0){
                iterator.remove();
            }
        }
        return listBean;
    }
    private void addToGroup(HomeAppGroupListBean groupListBean,HomeGrideListData data,HomeGrideListBean listBeanMine){

        if( listBeanMine != null && listBeanMine.searchDataByName(data.getName()) != null){
            return;
        }

            if(groupListBean.isHaveClassify(data.getClassify())){
                groupListBean.addHomeGridData(data);
            }else {
                HomeGrideListBean listBean = new HomeGrideListBean();
                listBean.getList().add(data);
                listBean.setClassify(data.getClassify());
                listBean.setTitle(HomeGrideListData.getClassifyName(data.getClassify()));
                groupListBean.getList().add(listBean);
            }
    }

    public void setWarnUnReadCount(int count){
        for(HomeGrideListData grideListData:list){
            if(grideListData.getName().equals("警情管理")){
                if(count == 0){
                    grideListData.setUnReadCountMsg("");
                } else if(count > 99){
                    grideListData.setUnReadCountMsg(">99");
                }else {
                    grideListData.setUnReadCountMsg(count+"");
                }
            }
        }
    }

    public void processData(){
        if(list == null || list.size() == 0)return;
        Iterator<HomeGrideListData> iterator = list.iterator();
        while (iterator.hasNext()){
            HomeGrideListData  itemData = iterator.next();
            if(!TextUtils.isEmpty(itemData.getId()) && itemData.getId().equals("APP_MEMBER_MANAGEMENT")){
//                if(!LoginBean.getInstance().getUserInfo().isRoleContans(UserInfoBean.FLOW_DYRY_ROLE_ID)){
//                    iterator.remove();
//                }
            }
        }

    }

}
